#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan jQuery ビューレット 機能

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_a"
__date__    = "2013/03/17"
__license__ = 'LGPL'

# ------------------------------------------------------------
## インポート
# ------------------------------------------------------------
# Python
from random import Random

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.viewlet import MikanDivViewlet, MikanImgViewlet, MikanJSViewlet, MikanCSSViewlet, MikanCDataViewlet, MikanViewlet

# ------------------------------------------------------------
## 変数
# ------------------------------------------------------------

# ------------------------------------------------------------
## 関数
# ------------------------------------------------------------

# ------------------------------------------------------------
## クラス
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan バナー スライド アイテム クラス
#
class MikanBannerSilderItem(object):

	def set_id(self, value):
		self.FID = value

	def get_id(self):
		return self.FID

	def set_image(self, value):
		self.FImage = value

	def get_image(self):
		return self.FImage

	def set_url(self, value):
		self.FURL = value

	def get_url(self):
		return self.FURL

	def set_message(self, value):
		self.FMessage = value

	def get_message(self):
		return self.FMessage

	def __init__(self, id=None, message=None, image=None, url=None):
		super(MikanBannerSilderItem, self).__init__()

		self.FID = id
		self.FMessage = message
		self.FImage = image
		self.FURL = url

	# ----------------------------------------------------
	## Property
	message = ESiPCProperty(get_message, set_message, None)
	id = ESiPCProperty(get_id, set_id, None)
	image = ESiPCProperty(get_image, set_image, None)
	url = ESiPCProperty(get_url, set_url, None)

# ------------------------------------------------------------
## Mikan バナー スライド ビューレット クラス
#
class MikanBannerSilderViewlet(MikanDivViewlet):

	def set_url(self, value):
		self.FURL = value

	def get_url(self):
		return self.FURL

	def get_items(self):
		return self.FItems

	def get_js(self):
		return self.FJS

	# ----------------------------------------------------
	## コンストラクタ
	#  @param parent 親Viewlet
	#  @param value ノード値
	def __init__(self, parent):
		super(MikanBannerSilderViewlet, self).__init__(parent)
		self.FURL = None
		self.FItems = {}
		self.clazz = "cycle-slideshow"
		self.id = "banner-cycle-slideshow"

		# キャプション
		caption = MikanDivViewlet(self.parent)
		caption.id = "alt-caption"
		caption.clazz = "cycle-caption"
		caption.sortkey = -100

		self.FJS = MikanJSViewlet(self.htmlview.body)

	def add_image(self, id, message=None, image=None, url=None):
		self.add_image_item(MikanBannerSilderItem(id, message, image, url))

	def add_image_item(self, item):
		self.items[item.id] = item

	def get_iImage_item(self, id):
		return self.items[id]

	def remove_image_item(self, id):
		del(self.item[id])

	def build_content(self):
		image_url = self.url
		if image_url == None:
			image_url = self.request.imageurl + "/%s"

		items = self.items.values()
		Random().shuffle(items)
		first = True
		images = """
var bannerimages = [ 
"""
		captions = """
var bannercaptions = [ 
"""
		for item in items:
			id = item.id
			image = item.image
			if image == None:
				image = id
			url = item.url
			message = item.message

			if first:

				img = MikanImgViewlet(self, alt=message, src=(image_url % image))
				if url:
					img.onclick = u"location.href='%s'" % (url)
				first = False
			images = images + "'" + image_url % image + """',
"""
			captions = captions + "'" + message + """',
"""
		images = images[1:-2]
		images = images + """
];
"""
		captions = captions[1:-2]
		captions = captions + """
];
"""
		self.js.nodevalue = images + captions

	# ----------------------------------------------------
	## 処理
	# @return Trueなら続行
	def perform(self):

		# コンテンツ構築
		self.build_content()

		# 継承元実行
		if not super(MikanBannerSilderViewlet, self).perform():
			return False

		# 属性設定
		self.set_element_attribute("data-cycle-fx", "tileBlind")
		self.set_element_attribute("data-cycle-loader", "true")
		self.set_element_attribute("data-cycle-timeout", "6000")
		self.set_element_attribute("data-cycle-caption", "#alt-caption")
		self.set_element_attribute("data-cycle-caption-template", "{{alt}}")

		# 実行結果
		return True

	# ----------------------------------------------------
	## Property
	js = ESiPCProperty(get_js, None, None)
	items = ESiPCProperty(get_items, None, None)
	url = ESiPCProperty(get_url, set_url, None)

# ------------------------------------------------------------
## Mikan バナー スライド ビューレット クラス
#
class MikanLogoSilderViewlet(MikanDivViewlet):

	def set_url(self, value):
		self.FURL = value

	def get_url(self):
		return self.FURL

	def get_items(self):
		return self.FItems

	def get_js(self):
		return self.FJS

	# ----------------------------------------------------
	## コンストラクタ
	#  @param parent 親Viewlet
	#  @param value ノード値
	def __init__(self, parent):
		super(MikanLogoSilderViewlet, self).__init__(parent)
		self.FURL = None
		self.FItems = {}
		self.clazz ="cycle-slideshow"
		self.id = "logo-cycle-slideshow"

		self.FJS = MikanJSViewlet(self.xhtmlview.body)

	def add_image(self, id, message=None, image=None, url=None):
		self.add_image_item(MikanBannerSilderItem(id, message, image, url))

	def add_image_item(self, item):
		self.items[item.id] = item

	def get_image_item(self, id):
		return self.items[id]

	def remove_image_item(self, id):
		del(self.item[id])

	def build_content(self):
		image_url = self.url
		if image_url == None:
			image_url = self.request.imageurl + "/%s"

		items = self.items.values()
		Random().shuffle(items)
		first = True
		node = """
var logoimages = [ 
"""
		for item in items:
			id = item.id
			image = item.image
			if image == None:
				image = id
			url = item.url

			if first:
				img = MikanImgViewlet(self, alt="", src=(image_url % image))
				img.clazz ="cycle-logo"
				if url:
					img.onclick =u"location.href='%s'" % (url)
				first = False
			else:
				node = node + "'" + image_url % image + """',
"""
		node = node[1:-2]
		node = node + """
];
"""
		self.js.nodevalue =node

	# ----------------------------------------------------
	## 処理
	# @return Trueなら続行
	def perform(self):

		# コンテンツ構築
		self.build_content()

		# 継承元実行
		if not super(MikanLogoSilderViewlet, self).perform():
			return False

		# 属性設定
		self.set_element_attribute("data-cycle-fx", "scrollHorz")
		self.set_element_attribute("data-cycle-timeout", "30000")
		self.set_element_attribute("data-cycle-loader", "wait")

		# 実行結果
		return True

	# ----------------------------------------------------
	## Property
	js = ESiPCProperty(get_js, None, None)
	items = ESiPCProperty(get_items, None, None)
	url = ESiPCProperty(get_url, set_url, None)

class MikanNotifViewlet(MikanViewlet):

	def set_msg(self, value):
		self.FMsg = value

	def get_msg(self):
		return self.FMsg

	def set_time(self, value):
		self.FTime = value

	def get_time(self):
		return self.FTime

	def set_type(self, value):
		self.FType = value

	def get_type(self):
		return self.FType

	def set_height(self, value):
		self.FHeight = value

	def get_height(self):
		return self.FHeight

	def set_width(self, value):
		self.FWidth = value

	def get_width(self):
		return self.FWidth

	def set_fade(self, value):
		self.FFade = value

	def get_fade(self):
		return self.FFade

	def set_opacity(self, value):
		self.FOpacity = value

	def get_opacity(self):
		return self.FOpacity

	def set_multiline(self, value):
		self.FMultiline = value

	def get_multiline(self):
		return self.FMultiline

	def set_color(self, value):
		self.FColor = value

	def get_color(self):
		return self.FColor

	def set_bgcolor(self, value):
		self.FBgcolor = value

	def get_bgcolor(self):
		return self.FBgcolor

	def set_position(self, value):
		self.FPosition = value

	def get_position(self):
		return self.FPosition

	def set_auto_hide(self, value):
		self.FAutohide = value

	def get_auto_hide(self):
		return self.FAutohide

	def __init__(self, parent, imageurl=None):
		super(MikanNotifViewlet, self).__init__(parent)

		self.FType = "warning"
		self.FMsg = None
		self.FHeight = None
		self.FWidth = None
		self.FFade = None
		self.FOpacity = None
		self.FMultiline = None
		self.FTime = None
		self.FColor = None
		self.FBgcolor = None
		self.FPosition = None
		self.FAutohide = None

	def prepare(self):
		params = u"type: \"%s\"" % self.type

		if self.msg:
			params = u"%s, %s: \"%s\"" % (params, u"msg", self.msg)
		if self.autohide:
			params = u"%s, %s: \"%s\"" % (params, u"autohide", self.autohide)
		if self.color:
			params = u"%s, %s: \"%s\"" % (params, u"color", self.color)
		if self.bgcolor:
			params = u"%s, %s: \"%s\"" % (params, u"bgcolor", self.bgcolor)
		if self.time:
			params = u"%s, %s: \"%s\"" % (params, u"time", self.time)
		if self.multiline:
			params = u"%s, %s: \"%s\"" % (params, u"multiline", self.multiline)
		if self.opacity:
			params = u"%s, %s: \"%s\"" % (params, u"opacity", self.opacity)
		if self.opacity:
			params = u"%s, %s: \"%s\"" % (params, u"opacity", self.opacity)
		if self.fade:
			params = u"%s, %s: \"%s\"" % (params, u"fade", self.fade)
		if self.height:
			params = u"%s, %s: \"%s\"" % (params, u"height", self.height)
		if self.width:
			params = u"%s, %s: \"%s\"" % (params, u"width", self.width)
		if self.position:
			params = u"%s, %s: \"%s\"" % (params, u"position", self.position)

		self.htmlview.loadedpage.append(u"notif({%s});" % params)

		return super(MikanNotifViewlet, self).prepare()

	# ----------------------------------------------------
	## Property
	autohide = ESiPCProperty(get_auto_hide, set_auto_hide, None)
	position = ESiPCProperty(get_position, set_position, None)
	color = ESiPCProperty(get_color, set_color, None)
	bgcolor = ESiPCProperty(get_bgcolor, set_bgcolor, None)
	time = ESiPCProperty(get_time, set_time, None)
	multiline = ESiPCProperty(get_multiline, set_multiline, None)
	opacity = ESiPCProperty(get_opacity, set_opacity, None)
	fade = ESiPCProperty(get_fade, set_fade, None)
	type = ESiPCProperty(get_type, set_type, None)
	msg = ESiPCProperty(get_msg, set_msg, None)
	height = ESiPCProperty(get_height, set_height, None)
	width = ESiPCProperty(get_width, set_width, None)

# ------------------------------------------------------------
## メイン関数
# ------------------------------------------------------------
## メイン処理
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
